
import 'package:Flutter_Demo/core/model/category_model.dart';
import 'package:Flutter_Demo/core/model/meal_model.dart';
import 'package:Flutter_Demo/core/services/json_parse.dart';
import 'package:Flutter_Demo/core/viewmodel/meal_view_model.dart';
import 'package:Flutter_Demo/ui/widgets/meal_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:collection/collection.dart';


//class MYMealContent extends StatefulWidget {
//  @override
//  _MYMealContentState createState() => _MYMealContentState();
//}
//
//class _MYMealContentState extends State<MYMealContent> {
//  @override
//  Widget build(BuildContext context) {
//    final category = ModalRoute.of(context).settings.arguments as MYCategoryModel;
//
//    List<MYMealModel> _meals = MYMealViewModel().meals;
//    if(_meals.length == 0) {
//      MYJsonParse.getMealData().then((value) {
//        _meals = value;
//      });
//    }
//
//    return Selector<MYMealViewModel, List<MYMealModel>>(
//      selector: (ctx, mealViewModel){
//        return _meals.where((meal) => meal.categories.contains(category.id)).toList();
//      },
//      shouldRebuild: (prev, next) => !ListEquality().equals(prev, next),
//      builder: (ctx, meals, child) {
//        return ListView.builder(
//          itemCount: meals.length,
//          itemBuilder: (ctx, index) {
//            return MYMealItem(meals[index]);
//          }
//        );
//      },
//    );
//  }
//}


class MYMealContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final category = ModalRoute.of(context).settings.arguments as MYCategoryModel;

    return Selector<MYMealViewModel, List<MYMealModel>>(
      selector: (ctx, mealViewModel){
        return mealViewModel.meals.where((meal) => meal.categories.contains(category.id)).toList();
      },
      shouldRebuild: (prev, next) => !ListEquality().equals(prev, next),
      builder: (ctx, meals, child) {
        return ListView.builder(
            itemCount: meals.length,
            itemBuilder: (ctx, index) {
              return MYMealItem(meals[index]);
            }
        );
      },
    );
  }
}





/*
class MYHomeContent extends StatefulWidget {
  @override
  _MYHomeContentState createState() => _MYHomeContentState();
}

class _MYHomeContentState extends State<MYHomeContent> {

  List<MYCategoryModel> _categories = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 加载数据
    MYJsonParse.getCategoryData().then((value) {
      _categories = value;
      print(value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: EdgeInsets.all(20.0),
      itemCount: _categories.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        // 2列
        crossAxisCount: 2,
        crossAxisSpacing: 20.0,
        mainAxisSpacing: 20.0,
        // 宽高比
        childAspectRatio: 1.5
      ),
      itemBuilder: (ctx, index) {
        return MYHomeCategoryItem(_categories[index]);
      },

    );
  }
}
 */


/*
// Consumer
class MYMealContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final category = ModalRoute.of(context).settings.arguments as MYCategoryModel;
    return Consumer<MYMealViewModel>(
      builder: (ctx, mealViewModel, child) {
        // 过滤函数
        final meals = mealViewModel.meals.where((meal) => meal.categories.contains(category.id)).toList();
        return ListView.builder(
          itemCount: meals.length,
          itemBuilder: (ctx, index) {
            return ListTile(title: Text(meals[index].title),);
          }
        );
      }
    );

  }
}
 */

